package com.projectexercise.book.controller;

import com.projectexercise.book.model.*;
import com.projectexercise.book.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
@RequestMapping("/book")
@RestController
@Slf4j
public class BookController {
    @Autowired
    private BookService bookService;
    @RequestMapping(value = "/addBook" , produces = "application/json;charset=utf8")
    public String addBook(BookInfo bookInfo){
        log.info("添加图书，接收到参数 bookInfo: {}", bookInfo);
        if(!StringUtils.hasLength(bookInfo.getBookName()) ||
                bookInfo.getCount() == null || bookInfo.getPrice() == null ||
                !StringUtils.hasLength(bookInfo.getAuthor()) ||
                !StringUtils.hasLength(bookInfo.getPublish()) ||
                bookInfo.getStatus() == null ){
            return "参数不合法";
        }
        try {
            Integer res = bookService.insertBook(bookInfo);
            if(res > 0){
                return "添加成功";
            }
        } catch (Exception e){
            log.error("添加图书异常", e);
        }
        return "添加失败";
    }

    @RequestMapping("/getBookListByPage")
    public Result<PageResponse<BookInfo>> getBookListByPage(PageRequest pageRequest, HttpSession session){
        log.info("查询图书列表，请求的参数：{}", pageRequest);
        PageResponse<BookInfo> pageResponse = bookService.getBookListByPage(pageRequest);
        return Result.success(pageResponse);
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId){
        log.info("根据Id查询图书信息: " + bookId);
        return bookService.queryBookById(bookId);
    }

    @RequestMapping(value = "/updateBookInfo", produces = "application/json;charset=utf8")
    public String updateBookInfo(BookInfo bookInfo){
        log.info("更新图书信息, bookInfo: {}", bookInfo);
        try {
            Integer result = bookService.updateBookInfo(bookInfo);
            if(result > 0){
                return "更新成功";
            }
        } catch (Exception e){
            log.error("更新失败：", e);
        }
        return "更新失败";
    }

    @RequestMapping(value = "/deleteBook", produces = "application/json;charset=utf8")
    public String deleteBook(Integer id){
        log.info("删除图书信息, id: " + id);
        BookInfo bookInfo = new BookInfo();
        bookInfo.setId(id);
        bookInfo.setStatus(0);
        try {
            Integer result = bookService.updateBookInfo(bookInfo);
            if(result > 0){
                return "删除成功";
            }
        } catch (Exception e){
            log.error("删除失败, e: ", e);
        }
        return "删除失败";
    }

    @RequestMapping(value = "/batchDeleteBook", produces = "application/json;charset=utf8")
    public String batchDeleteBook(@RequestParam @Param("ids") List<Integer> ids){
        log.info("批量删除 ids: " + ids);
        try {
            Integer result = bookService.batchDeleteBook(ids);
            if(result > 0){
                return "批量删除成功";
            }
        } catch (Exception e){
            log.error("批量删除失败 e", e);
        }
        return "批量删除失败";
    }
}
